package com.reabam.tryshopping.h5.reabampos;

import android.app.Activity;
import android.os.Handler;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;



/**
 * 设置全屏,适用flutter项目的android, 为了适配:美团收银机,商米T2 无法滑动弹出底部导航栏
 * 添加: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
 * 复制到Flutter项目android中,在MainActivity中修改:

 @Override
 public void onCreate(Bundle savedInstanceState) {
    //
    FlutterAndroidFullScreenUtils.setFullScreen(this);
    //
    super.onCreate(savedInstanceState);
 }

 @Override
 public void onDestroy() {
    super.onDestroy();
    FlutterAndroidFullScreenUtils.
 }

 }

 *
 */
public class FlutterAndroidFullScreenUtils {

    private static Handler handler;
    /*
    使用android新系统(向下兼容android 20)
    如:美团收银机,商米T2
     */
    public static void setFullScreen(
            Activity activity
    ){

        //适用: 美团收银机
        if("MT-S4Sp".equalsIgnoreCase(android.os.Build.MODEL) || "T2".equalsIgnoreCase(android.os.Build.MODEL)){
            WindowInsetsControllerCompat windowInsetsController =
                    WindowCompat.getInsetsController(activity.getWindow(), activity.getWindow().getDecorView());
            // Configure the behavior of the hidden system bars.
            windowInsetsController.setSystemBarsBehavior(
                    WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            );

            //显示顶部状态栏和底部导航栏
            //windowInsetsController.show(WindowInsetsCompat.Type.systemBars());
            //隐藏顶部状态栏和底部导航栏
            windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());

            //当WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE 无效不会重新自动隐藏时,设置监听,延时关闭
            boolean isListenerShowSystemBars=true;
            if(isListenerShowSystemBars){
                //监听
                ViewCompat.setOnApplyWindowInsetsListener(
                        activity.getWindow().getDecorView(),
                        (view, windowInsets) -> {

                            if(windowInsets.isVisible(WindowInsetsCompat.Type.ime())){
                                //键盘 https://developer.android.google.cn/develop/ui/views/layout/sw-keyboard?hl=zh-cn
                                //键盘弹出也会伴随底部导航栏显示,因此要过滤键盘弹出时,不延时隐藏

                            }else if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars())) {

                                //
                                if(handler==null){
                                    handler=new Handler();
                                }

                                //
                                handler.removeCallbacksAndMessages(null);

                                //
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
                                    }
                                },3000);


                            } else {

                            }
                            return ViewCompat.onApplyWindowInsets(view, windowInsets);
                        });
            }
        }



    }



    public static void release(){
        if(handler!=null){
            handler.removeCallbacksAndMessages(null);
            handler=null;
        }
    }


}
